<?php

  /*

      TravianWAP
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/travianwap/
      Contact: uroskn@gmail.com

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU  General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU  General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

  */

  define("TRAVIANWAP_PRIMARY", 1);
  define("TRAVIANWAP_SEC",     3);
  define("TRAVIANWAP_REM",     1);
  define("TRAVIANWAP_VERSION", TRAVIANWAP_PRIMARY.".".TRAVIANWAP_SEC.".".TRAVIANWAP_REM."b");
  define("TRAVIANWAP_DATE",    1221495750);
  define("UPDATE_CHECK_SITE",  "http://n00bz.biz/travianwap/updatecheck.php");
  
  function process_number($txt)
  {
    if (defined("USE_PREFIXES"))
    {
      $kpos = strpos($txt, "k");
      if ($kpos)
      {
        $data1 = substr($txt, 0, $kpos)."000";
        $lpos = $kpos;
      }
      $pos = strpos($txt, "h");
      if ($pos)
      {
         $lpos = $pos;
         if (!$kpos) $kpos = -1;
         $data2 = substr($txt, $kpos + 1);
         $data2 = substr($data2, 0, strpos($data2, 'h'))."00";
      }
      $data3 = substr($txt, $lpos + 1);
      if ((!$kpos) && (!$pos)) return $txt;
      else return ((int)$data1 + (int)$data2 + (int)$data3);
    }
    else return $txt;
  }

  // Used for printing TP timestamps.
  function pdate($timestamp, $pseconds = True)
  {
    $hours = (int)($timestamp / 3600);
    $timestamp = $timestamp - ($hours * 3600);
    $minutes = (int)($timestamp / 60);
    $seconds = $timestamp - ($minutes * 60);
    if ($hours < 10) $hours = "0$hours";
    if ($minutes < 10) $minutes = "0$minutes";
    if ($seconds < 10) $seconds = "0$seconds";
    if ($pseconds) $seconds = ":$seconds";
    else unset($seconds);
    return "$hours:$minutes$seconds";
  }
  
  function ps($separator = "&amp;")
  {
    if (defined(SID)) return $separator.htmlspecialchars(SID);
    else return "";
  }
  
  function print_menu($module, $_all = False)
  {
    global $tw_link;
    if ($_all) $_module = M_ALL;
    else $_module = $module;
    for ($i = 0; $i < count($tw_link[$_module]); $i++)
    {
      if ($_module == M_ALL)
      {
        for ($x = 0; $x < count($tw_link[$_module][$i][3]); $x++) if ($tw_link[$_module][$i][3][$x] == $module) continue 2;
      }
      if (!empty($tw_link[$_module][$i][0])) print_link($tw_link[$_module][$i][0].ps('&'), $tw_link[$_module][$i][1], "<br/>", True);
    }
    if (!$_all) print_menu($module, TRUE);
  }

  define("MAIN_UNIT", 1);

  $req = array_merge($_POST, $_GET);
  //error_reporting(0);
  include("travian.php");

  session_start();

  // For quick access trough mobile phones (not required to login for every shit).
  if (!empty($_GET["user"]))
  {
    $_SESSION = travian_create($_GET["user"], $_GET["pass"], $_GET["server"]);
    if (!$_SESSION)
    {
      $location = $_SERVER["SERVER_NAME"];
      header("Location: http://{$_SERVER["SERVER_NAME"]}".str_replace('\\', '/', dirname($_SERVER["SCRIPT_NAME"]))."/index.php?message=Unable%20to%20login!");
    }
  }

  if (empty($_SESSION["cookie"]))
  {
   header("Location: http://{$_SERVER["SERVER_NAME"]}".dirname($_SERVER["SCRIPT_NAME"])."/index.php?message=Not%20logged%20in!");
   exit();
  }

  http_restore_cookies($_SESSION["cookie"]);
  $response = http_get("http://{$_SESSION["serv"]}/dorf1.php");
  $txt = $response["body"];
  if (travian_logged_in($response["body"]) == False)
  {
    header("Location: http://{$_SERVER["SERVER_NAME"]}".dirname($_SERVER["SCRIPT_NAME"])."/index.php?message=Session%20timed%20out!");
    exit();
  }
  if (empty($_SESSION["village"])) $_SESSION["village"] = travian_extract_village_name($txt, True);

  function printtop($txt)
  {
    echo "<b>".$_SESSION["village"]."</b><br/>";
    $res = travian_extract_resource_count($txt, True);
    echo "{$res[0][0]}-{$res[1][0]}-{$res[2][0]}/{$res[2][1]}<br/>".
         "{$res[3][0]}/{$res[3][1]}<br/><br/>";
  }

  require("display.php");
  define("M_ALL", "#ALL#");
  require("config.php");

  $req["mod"] = basename($req["mod"]);
  include("modules/{$req["mod"]}.php");
  if (!Defined("MODULE_OK"))
  {
    echo "<b>TravianWAP error!</b><br/><br/>Unable to load specific module!<br/><br/>";
    print_link("main.php?mod=main".ps(), "Back", "<br/><br/>");
  }
  
  print_menu($req["mod"]);

  echo "<br/>";

?>

